{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 线性回归"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 线性回归是一种最简单的机器学习模型，试图学习一个通过属性的线性组合来进行预测的函数，对于线性组合来说，有两种求解方式\n",
    "\n",
    "1. 最小二乘法\n",
    "2. 梯度下降"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "from sklearn.datasets import make_regression\n",
    "\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 构造数据集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.collections.PathCollection at 0x10f0bd240>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYMAAAD8CAYAAACVZ8iyAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAG/lJREFUeJzt3X+MHOddx/HP1+dNckmBSxWTOhubGOQ6ShpiK6cQMKAm\nLXWg0FwTaF0BLaKqQYRfVWVkU4kEQVQLUyJRoGCgalFDU9MkrkVSTFJbVKpwkzN2mjixwW2axNvQ\nXEmuJfIlnM9f/rhZe29vZmdmZ+Zmd+b9kk6+nZ2bfVaJ5jvP93me72PuLgBAvS0ruwEAgPIRDAAA\nBAMAAMEAACCCAQBABAMAgAgGAAARDAAAIhgAACQtL7sBSV1yySV+xRVXlN0MABgqhw4d+ra7r4g7\nb2iCwRVXXKHJycmymwEAQ8XMnk1yHmkiAADBAABAMAAAiGAAABDBAACgIZpNBAB1sudwSzv3Hdc3\np2d02diotm5ap4kNzcI+j2AAAANmz+GWtt//hGZm5y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P/07ZbSqKmV1gZo+a2ePB\nd/3DvD+DYBDuYUlvcvcflvSfkraX3J4i9aovNfTMbETSX0r6aUlXSXqPmV1VbqsK9UnVp97XaUkf\ncverJN0g6fYK/7d9TdJN7n6tpPWSbg6m6OeGYBDC3f/V3U8HLw9q4UK5SklbX2oIXS/phLt/3d3/\nT9K9km4puU2FcfcvSXop9sQKcPcX3P0/gt//V9LT6lHeZpj5vFeCl43gJ9cBX4JBvF8VayGGWVPS\n8x2ve9bDwnAysyskbdD8YtZKMrORYO3Wi5Iedvdcv2ulN7fpJUnNJTP7sOa7ovcsZdvy1md9KWAo\nmNnrNL+u6Xfd/btlt6co7j4naX0whvmAmb3J3XMbG6ptMIiruWRmvyLpZyW9xYd8/m0/9aUqpCVp\nVcfrxPWwMPjMrKH5QHCPu99fdnuWgrtPm9kBzY8N5RYMSBOFMLObJf2epHe4+6my24NMHpO01szW\nmNl5kjZL2ltym5CDYJfEv5f0tLv/WdntKZKZrWjPajSzUUk/JelYnp9BMAj3F5K+R9LD3dt2Vk2P\n+lKVEEwE+E1J+zQ/wLjb3Y+W26rimNlnJP27pHVmdtLM3l92mwq0UdIvS7qpY4vdnym7UQVZKemA\nmX1V8w84D7v7P+f5AaxABgDQMwAAEAwAACIYAABEMAAAiGAAABDBAAAgggEAQAQDAICk/wetR8jz\nwcRNiAAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x10eef77f0>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "X,y = make_regression(n_samples=100,noise=20,n_features=1)\n",
    "plt.scatter(X,y)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 使用最小二乘法来求解"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 最小二乘法是一种求解释一种基于均方误差来求解的一种方法，在线性回归中，最小二乘法就是试图找到一条直线，使所有的样本到直线上的欧式距离最小\n",
    "\n",
    "$Ax = b$\n",
    "\n",
    "$A^TAx = A^Tb$\n",
    "\n",
    "则\n",
    "\n",
    "$x = (A^TA)^{-1}A^Tb$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 在这里，x就是我们所要求得的参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [],
   "source": [
    "ones = np.ones(X.shape[0])\n",
    "A = np.insert(X, 0, 1, axis=1)  # 特征"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [],
   "source": [
    "b = y.reshape(-1,1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 使用最小二乘法求解\n",
    "\n",
    "$Ax=b$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = np.linalg.inv(A.T @ A) @ A.T @ b"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 那么X为"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[  1.86962816],\n",
       "       [ 52.04612077]])"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 也就是我们需要求解的参数，我们将该方程画图出来"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<matplotlib.lines.Line2D at 0x1104cafd0>]"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYMAAAD8CAYAAACVZ8iyAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzt3XuUnFW55/HvQ6cJDQhBCBg6CR3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Y0zs4fN7Jnkf8P/\nXO0+xcXM9jCzx8zsieR3/WqlP0PBINgDwJHufjTwF2BOlfsTp3z1peqembUA3wM+AhwBXGBmR1S3\nV7G6jeap99UPXOnuRwAnApc18P+3O4FT3f0Y4FjgjGSKfsUoGARw99+5e3/y4QoyN8o1lGLrS9Wh\nE4D17v6Cu78D/ByYXuU+xcbdfw9sKXhhA3D3V9z98eTfbwPPkqe8TT3zhL8nH7Ym/6nogq+CQWEX\nob0Q9awd+Gva47z1sKQ+mVkHMJnEZtaGZGYtyb1bm4EH3L2i37WhD7fJJ0rNJTP7Momh6B3D2bdK\nK7G+lEhdMLO9Sexr+qK7v1Xt/sTF3QeAY5NrmIvM7Eh3r9jaUNMGg0I1l8zsvwPTgNO8zvNvS6kv\n1UB6gHFpjyPXw5LaZ2atJALBHe7+q2r3Zzi4+zYze5jE2lDFgoGmiQKY2RnAVcBZ7r692v2RsvwZ\nONTMJpjZ7sAMYHGV+yQVkDwl8UfAs+7+zWr3J05mNjqV1WhmbcCHgecq+RkKBsG+C7wLeCD72M5G\nk6e+VENIJgJ8AVhKYoHxbndfW91excfM7gT+BEw0s41mdnG1+xSjKcCngFPTjtj9x2p3KiZjgIfN\n7EkSP3AecPcllfwA7UAWERGNDERERMFARERQMBARERQMREQEBQMREUHBQEREUDAQEREUDEREBPj/\nGtlEw5G5gk4AAAAASUVORK5CYII=\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x1102c6e48>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.scatter(X,y)\n",
    "\n",
    "y_pred = A @ x\n",
    "\n",
    "plt.plot(X,y_pred,'r--')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 最小二乘法是非常常用的算法来求解 线性回归问题"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 使用梯度下降来求解"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 175,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "class LinearRegression(object):\n",
    "    def __init__(self):\n",
    "        pass\n",
    "    def fit(self,X,y):\n",
    "        X = np.insert(X, 0, 1, axis=1)\n",
    "        y = y.reshape(-1,1)\n",
    "        self.w = np.random.randn(X.shape[1],1)\n",
    "        \n",
    "        for _ in range(50):\n",
    "        \n",
    "            y_pred = X @ self.w  # 100 * 1\n",
    "            \n",
    "            \n",
    "            mse = np.mean(0.5*(y_pred-y)**2)\n",
    "\n",
    "            grad_w = X.T @ (y_pred - y)\n",
    "            \n",
    "            self.w -= 0.01 * grad_w\n",
    "            \n",
    "            print(_,mse,self.w[0][0],self.w[1][0])\n",
    "            \n",
    "    def predict(self,X):\n",
    "        X = np.insert(X, 0, 1, axis=1)\n",
    "        return X @ self.w"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 176,
   "metadata": {},
   "outputs": [],
   "source": [
    "lr = LinearRegression()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 177,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 [[ 1734.75699056]] 0.955185053008 60.3923330382\n",
      "1 [[ 18445.5964029]] 1.90492609609 44.6473973352\n",
      "2 [[ 10180.76495859]] 1.56316980672 48.774385072\n",
      "3 [[ 12077.05088666]] 1.66203080946 47.6911839011\n",
      "4 [[ 11560.95950818]] 1.6351668849 47.9756328586\n",
      "5 [[ 11695.19197897]] 1.64231165918 47.9009223224\n",
      "6 [[ 11659.849021]] 1.64042618749 47.9205464437\n",
      "7 [[ 11669.12627968]] 1.64092231823 47.9153916634\n",
      "8 [[ 11666.68896292]] 1.64079191042 47.9167457126\n",
      "9 [[ 11667.32916289]] 1.64082617423 47.9163900319\n",
      "10 [[ 11667.16099373]] 1.640817173 47.916483462\n",
      "11 [[ 11667.2051682]] 1.64081953752 47.9164589198\n",
      "12 [[ 11667.19356445]] 1.6408189164 47.9164653665\n",
      "13 [[ 11667.19661252]] 1.64081907956 47.9164636731\n",
      "14 [[ 11667.19581185]] 1.6408190367 47.9164641179\n",
      "15 [[ 11667.19602217]] 1.64081904796 47.9164640011\n",
      "16 [[ 11667.19596692]] 1.640819045 47.9164640318\n",
      "17 [[ 11667.19598144]] 1.64081904578 47.9164640237\n",
      "18 [[ 11667.19597763]] 1.64081904557 47.9164640258\n",
      "19 [[ 11667.19597863]] 1.64081904563 47.9164640253\n",
      "20 [[ 11667.19597836]] 1.64081904561 47.9164640254\n",
      "21 [[ 11667.19597843]] 1.64081904562 47.9164640254\n",
      "22 [[ 11667.19597841]] 1.64081904562 47.9164640254\n",
      "23 [[ 11667.19597842]] 1.64081904562 47.9164640254\n",
      "24 [[ 11667.19597842]] 1.64081904562 47.9164640254\n",
      "25 [[ 11667.19597842]] 1.64081904562 47.9164640254\n",
      "26 [[ 11667.19597842]] 1.64081904562 47.9164640254\n",
      "27 [[ 11667.19597842]] 1.64081904562 47.9164640254\n",
      "28 [[ 11667.19597842]] 1.64081904562 47.9164640254\n",
      "29 [[ 11667.19597842]] 1.64081904562 47.9164640254\n",
      "30 [[ 11667.19597842]] 1.64081904562 47.9164640254\n",
      "31 [[ 11667.19597842]] 1.64081904562 47.9164640254\n",
      "32 [[ 11667.19597842]] 1.64081904562 47.9164640254\n",
      "33 [[ 11667.19597842]] 1.64081904562 47.9164640254\n",
      "34 [[ 11667.19597842]] 1.64081904562 47.9164640254\n",
      "35 [[ 11667.19597842]] 1.64081904562 47.9164640254\n",
      "36 [[ 11667.19597842]] 1.64081904562 47.9164640254\n",
      "37 [[ 11667.19597842]] 1.64081904562 47.9164640254\n",
      "38 [[ 11667.19597842]] 1.64081904562 47.9164640254\n",
      "39 [[ 11667.19597842]] 1.64081904562 47.9164640254\n",
      "40 [[ 11667.19597842]] 1.64081904562 47.9164640254\n",
      "41 [[ 11667.19597842]] 1.64081904562 47.9164640254\n",
      "42 [[ 11667.19597842]] 1.64081904562 47.9164640254\n",
      "43 [[ 11667.19597842]] 1.64081904562 47.9164640254\n",
      "44 [[ 11667.19597842]] 1.64081904562 47.9164640254\n",
      "45 [[ 11667.19597842]] 1.64081904562 47.9164640254\n",
      "46 [[ 11667.19597842]] 1.64081904562 47.9164640254\n",
      "47 [[ 11667.19597842]] 1.64081904562 47.9164640254\n",
      "48 [[ 11667.19597842]] 1.64081904562 47.9164640254\n",
      "49 [[ 11667.19597842]] 1.64081904562 47.9164640254\n"
     ]
    }
   ],
   "source": [
    "lr.fit(X,y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 178,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "y_pred = lr.predict(X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 179,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<matplotlib.lines.Line2D at 0x11092c400>]"
      ]
     },
     "execution_count": 179,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYMAAAD8CAYAAACVZ8iyAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzt3XuYXFWZ7/Hvm6YTOqA0SATSIQQRwtVJpI144pwxgCYg\nShNUgjrq4Bg4ggjyBJIDSjyICcYBRRk1c7yLQATSRmImXJI5jnm4dQi3BDIGuaUIEC6RITSh03nP\nH1WVVFftXbXrsntXV/0+z8ND16pdu1Y96H73Xutd7zJ3R0REmtuwpDsgIiLJUzAQEREFAxERUTAQ\nEREUDEREBAUDERFBwUBERFAwEBERFAxERATYLekORLXvvvv6uHHjku6GiMiQsnr16pfcfVSp44ZM\nMBg3bhw9PT1Jd0NEZEgxs6ejHKdhIhERUTAQEREFAxERQcFARERQMBAREYZQNpGISDPpXpNiwfL1\nPLell9HtbcyaOp6uiR2xfZ+CgYhInelek2LOrY/Q29cPQGpLL3NufQQgtoCgYSIRkTqzYPn6nYEg\nq7evnwXL18f2nQoGIiJ15rktvWW114KCgYhInRnd3lZWey0oGIiI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ComR1sZsOBGcCShPskNZDZ\nJfGnwGPufnXS/YmTmY3KZjWaWRvwYeDxWn6HgkGwHwJvA+7I37az0RSpL9UQMokA5wHLSU8wLnL3\ntcn2Kj5mdgNwNzDezDaa2ReT7lOMJgP/CByfs8XuyUl3KiYHACvN7GHSNzh3uPtttfwCrUAWERE9\nGYiIiIKBiIigYCAiIigYiIgICgYiIoKCgYiIoGAgIiIoGIiICPD/AQrqaYslsfJuAAAAAElFTkSu\nQmCC\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x11092c518>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.scatter(X,y)\n",
    "\n",
    "plt.plot(X,y_pred,'r--')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 可以发现，使用梯度下降算法得到了与最小二乘法差不多的结果。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 在周老师的书中，还提出了最优解的闭式解\n",
    "\n",
    "* 参见西瓜书 54页"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 146,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "x_mean = np.mean(X)\n",
    "Y = y.reshape(-1,1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 149,
   "metadata": {},
   "outputs": [],
   "source": [
    "w = np.sum(Y*(X-x_mean))/(np.sum(X**2)-1./X.shape[0]*(np.sum(X))**2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 158,
   "metadata": {},
   "outputs": [],
   "source": [
    "b = np.sum(Y-w*X)/X.shape[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 160,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1.869628157291537, 52.046120767103673)"
      ]
     },
     "execution_count": 160,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b,w"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 可见，这三种方式都是求线性规划的方法。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 总结\n",
    "\n",
    "* 使用最小二乘法来求解 $A^TAx=A^Tb$\n",
    "\n",
    "* 使用梯度下降算法\n",
    "\n",
    "* 使用闭包解\n",
    "\n",
    "## 参考\n",
    "\n",
    "* 54、55页周志华老师的西瓜书"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
